Dr. T's Coding Advice
To maximize code readability and maintainability, both for others and for yourself, here are some tips:
- Use descriptive variable names
- Avoid generic names like i, j, k, temp, etc. in favor of variable names that describe what the variable contains or represents.
- If you have a for loop that iterates over a list of children solutions, name your loop variable something like child instead of i.
- Comment above blocks or lines of non-obvious code
- If you have a complex one-liner (ex. Python list comprehension or complex single-line logic expression) in your code, you most likely need a comment explaining what it does.
- If you have confusing or non-intuitive variable or function names, you may consider adding descriptive comments.
- Comment within the code of a function
- The code of not only your main function, but also the code for the functions called within main and all other code indirectly called by main, should be clear and understandable. This will facilitate manually tracing your function calls from the main function of your code.
- Don't copy and paste identical or slightly modified code
- In general, if you can solve a problem with a copy-and-paste of code (possibly with some minor tweaks), then you can and should solve that problem by encapsulating the code in a function and calling it from both the original location and the new location.
- Adhere to a high-quality programming style guide such as:
For those of you programming in Python, you should write your code "The Pythonic Way." Alas, this way of writing code is more of a culture than a strict standard and the closest thing we can find to a guide is the Zen of Python.